Summary

Row

Day

2020-05-02

Total confirmed cases

435

Total recovered cases

152(34.94%)

Succumbed cases

22(5.06%)

Total active cases

261(60%)

Column

Total cumulative cases in: Kenya

Daily count

Row

Day

2020-05-02

Confirmed cases for the day

24

Recovered cases for the day

2

Succumbed cases for the day

1

Column

Daily confirmed cases in Kenya

Column

Daily recovered cases in Kenya

Daily succumbed cases in Kenya

Combined the above plots using ggplot2 instead of plotly

County stats

Column

Number of confirmed cases per county

county date cases_today total_cases
Nairobi 2020-05-02 17 266
Mombasa 2020-05-02 5 129
Migori 2020-05-02 2 2
Kilifi 2020-05-02 0 8
Mandera 2020-05-02 0 8
Kiambu 2020-05-02 0 5
Kajiado 2020-05-02 0 3
Kitui 2020-05-02 0 3
Kwale 2020-05-02 0 2
Nakuru 2020-05-02 0 2
Siaya 2020-05-02 0 2
Wajir 2020-05-02 0 2
Homa_Bay 2020-05-02 0 1
Kakamega 2020-05-02 0 1
Uasin-Gishu 2020-05-02 0 1

Global stats

Row

Day

2020-05-02

Global confirmed cases

3,427,343

Recovered cases

1,093,112(31.89%)

Succumbed cases

243,808(7.11%)

Row

Daily cumulative cases globally:

Africa stats

Row

Day

2020-05-02

Confirmed cases in Africa

42,772

Recovered cases

14,129(33.03%)

Succumbed cases

1,759(4.11%)

Column

Daily cumulative cases in Africa:

Protective measures

Row

How to protect yourself and others

Here are some guidelines from the WHO

Column

Wash your hands frequently

Regularly and thoroughly clean your hands with an alcohol-based hand rub or wash them with soap and water.

Why? Washing your hands with soap and water or using alcohol-based hand rub kills viruses that may be on your hands.

Maintain social distancing

Maintain at least 1 metre (3 feet) distance between yourself and anyone who is coughing or sneezing.

Why? When someone coughs or sneezes they spray small liquid droplets from their nose or mouth which may contain virus. If you are too close, you can breathe in the droplets, including the COVID-19 virus if the person coughing has the disease.

Column

If you have fever, cough and difficulty breathing, seek medical care early

Stay home if you feel unwell. If you have a fever, cough and difficulty breathing, seek medical attention and call in advance. Follow the directions of your local health authority.

Why? National and local authorities will have the most up to date information on the situation in your area. Calling in advance will allow your health care provider to quickly direct you to the right health facility. This will also protect you and help prevent spread of viruses and other infections.

Stay informed and follow advice given by your healthcare provider,your national and local public health authority

Kindly visit: https://www.who.int/emergencies/diseases/novel-coronavirus-2019 for more information about the Coronavirus disease (COVID-19) Pandemic.

About

The COVID-19 in Kenya Dashboard

Last updated: 02 May

This Coronavirus dashboard provides an overview of the 2019 Novel Coronavirus COVID-19 (2019-nCoV) epidemic. This dashboard mainly focuses on Kenya but also gives some brief statistics about the prevalence of the pandemic in the world and Africa. This dashboard is built with R using the R Makrdown framework and was adapted from this dashboard by Rami Krispin.

Data

The input data for this dashboard is the coronavirus R package (dev version) by Rami Krispin and the Ministry of Health, Kenya.The data and dashboard is refreshed on a daily bases. The raw data pulled from the Johns Hopkins University Center for Systems Science and Engineering (JHU CCSE) Coronavirus repository

Contribution

The shape files for the county stats tab were contributed by Maggie Wanjiru. Thank you!

---
## https://rmarkdown.rstudio.com/flexdashboard/using.html#multiple_pages
#runtime: shiny
title: "COVID-19 in Kenya"
author: "R.ic"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    source_code: embed
    vertical_layout : fill ##Specify fill to vertically re-size charts so they completely fill the page and scroll to layout charts at their natural height, scrolling the page if necessary.

---


```{r}
#--------------------Required Packages----------------------------

# we'll be using the coronavirus() package.
#The input data for this dashboard is the dataset available from the {coronavirus} R package. Make sure to download the development version of the package to have the latest data:

#install.packages("devtools")
#devtools::install_github("RamiKrispin/coronavirus")

## To get the latest stats, use the function:  update_datasets()
## which will prompt you to type in n/Y to download the Dev version. A restart of the R session may also be needed to access the updated dataset
suppressPackageStartupMessages({
  library(shiny)
  library(tidyverse)
  library(flexdashboard)
  library(plotly)
  library(lubridate)
  library(coronavirus)
  library(knitr)
  library(countrycode)
  library(DT)
  library(rgdal)
  library(viridis)
  library(readxl)
  library(magrittr)
  library(formattable)
  #library(leaflet)
  #library(geojsonio)
  #library(sf)
 
})


```



```{r}
#------------------------Slicing and dicing the data-----------

covid_set=coronavirus
#View(covid_set)

##I won't be needing the Province.State,Lat and Long columns, for now at least.

covid_set=covid_set %>% select(-c(Province.State,Lat,Long))

## we'll then be looking to widen the data, such that each incident ie Confirmed,Death and Recovered has it's own column. This will go a long way in making analysis easier.

covid_set=covid_set %>% group_by(Country.Region,date,type) %>%
  
  summarise(total=sum(cases)) %>% 
  
  pivot_wider(names_from = type,values_from = total)%>%
  
  # setting a column to null drops it
  ungroup() %>% mutate(daily_conf=confirmed,confirmed=NULL,daily_deaths=death,death=NULL,daily_recov=recovered,recovered=NULL,daily_active=daily_conf-daily_deaths-daily_recov) %>%
  
group_by(Country.Region) %>% mutate(cum_conf=cumsum(daily_conf),cum_deaths=cumsum(daily_deaths),cum_recov=cumsum(daily_recov),cum_active=cum_conf-cum_deaths-cum_recov)
## I have then added a Week column to see how the cases change on a weekly bases

##?cut.Date
covid_set=covid_set %>% mutate(Week=as.Date(cut(date,breaks = "week",start.on.monday=FALSE)))



# great! Our dataset is in a desired format to permit further analysis

## My country is Kenya, so I will build the dashboard based on Kenya. Feel free to modify it.

covid_set_ke=covid_set %>% filter(Country.Region=="Kenya")

## day of first case confirmed
day=(covid_set_ke %>% ungroup() %>% filter(daily_conf>0) %>% slice(1) %>% select(date))


##
#To define a page just use a level 1 markdown header (==================).


# colors for the value boxes were obtaned from:https://material.io/design/color/the-color-system.html#tools-for-picking-colors
```



Summary
=======================================================================

Row {data-width=400}
-----------------------------------------------------------------------

### Day

```{r}
day=last(covid_set$date)

valueBox(
  day,
  icon="far fa-calendar-plus",
  color="#5C6BC0"
  
)
```

### Total confirmed cases{.value-box}


```{r}
#conf=last(covid_set_ke$cum_conf)
valueBox(
  value=last(covid_set_ke$cum_conf),
  caption = paste("Total confirmed cases"),
   icon = "fas fa-user-md",
  color="#7B1FA2")

##",'\n','\t',"(+",last(covid_set_ke$daily_conf)," from yesterday", ")",sep = "")
```

### Total recovered cases

```{r}
recov=last(covid_set_ke$cum_recov)


recov_rate=round(100*last(covid_set_ke$cum_recov)/last(covid_set_ke$cum_conf),2)

valueBox(
  cat(format(recov,big.mark = ","),"(",recov_rate,"%",")",sep=""),
  icon="fas fa-heartbeat",
  color="#00C853"

)


```


### Succumbed cases 

```{r}
deaths=last(covid_set_ke$cum_deaths)
death_rate=round(100*last(covid_set_ke$cum_deaths)/last(covid_set_ke$cum_conf),2)

valueBox(
  cat(format(deaths,big.mark = ","),"(",death_rate,"%",")",sep=""),
  icon="ion-heart-half-outline",
  color="#D50000"
)

```




### Total active cases

```{r}
active=last(covid_set_ke$cum_active)

active_rate=round(100*last(covid_set_ke$cum_active)/last(covid_set_ke$cum_conf),2)

valueBox(
  cat(format(active,big.mark = ","),"(",active_rate,"%",")",sep=""),
  icon="fas fa-hospital",
  color="#37474F"
)
#cat(recov,"(",recov_rate,"%",")",sep="")
```

Column
-----------------------------------------------------------------------

### **Total cumulative cases in: ** Kenya


```{r,fig.width=11.5, fig.height=7}
##,fig.width=10, fig.height=7

#### day of first case confirmed
day=(covid_set_ke %>% ungroup() %>% filter(daily_conf>0) %>% slice(1) %>% select(date))
theme_set(theme_light())
#?scale_color_manual
colors=c("confirmed"="blue","succumbed"="red","recovered"="green")

##fig_k=covid_set_ke %>% group_by(Week) %>% summarise(confirmed=max(cum_conf),succumbed=max(cum_deaths),recovered=max(cum_recov)) 
  fig=covid_set_ke %>% ungroup() %>% filter(date>=day$date) %>% 
  ggplot(aes(x=date))+
  geom_point(mapping = aes(y=cum_conf,color="confirmed"))+
  geom_point(mapping = aes(y=cum_deaths,color="succumbed"),alpha=0.6)+
  geom_point(mapping=aes(y=cum_recov,color="recovered"))+
  geom_line(mapping=aes(y=cum_conf,color="confirmed"))+
  geom_line(mapping=aes(y=cum_deaths,color="succumbed"))+
  geom_line(mapping=aes(y=cum_recov,color="recovered"))+
#geom_text(aes(x=last(fig_k$Week),y=last(fig_k$confirmed),label=last(fig_k$confirmed)),alpha=1,size=4,nudge_x=0,nudge_y=2)+
  
#geom_text(aes(x=nth(fig_k$Week,-2),y=nth(fig_k$confirmed,-2),label=nth(fig_k$confirmed,-2)),alpha=1,size=4,nudge_x=0,nudge_y=2)+
    
  labs(y="Cumulative number of cases",
       x="Day",
       ggtitle(""),
       color="")+
  scale_color_manual(values = colors)
ggplotly(fig,tooltip = c("cum_conf","cum_recov","cum_deaths")) %>% 
  layout(hovermode="compared")
```





Daily count
=======================================================================


Row {data-width=400}
-----------------------------------------------------------------------

### Day

```{r}
day=last(covid_set$date)

valueBox(
  day,
   icon="far fa-calendar-plus",
  color="#5C6BC0"
)
```

### Confirmed cases for the day

```{r}
confirmed=last(covid_set_ke$daily_conf)
valueBox(
  format(confirmed,big.mark = ","),
   icon = "fas fa-user-md",
   color="#7B1FA2"
 
)
```


### Recovered cases for the day

```{r}
recovered=last(covid_set_ke$daily_recov)
valueBox(
  format(recovered,big.mark = ","),
  icon="fas fa-heartbeat",
  color="#00C853"
 
)
```






### Succumbed cases for the day

```{r}
succumbed=last(covid_set_ke$daily_deaths)
valueBox(
  format(succumbed,big.mark = ","),
  color="#D50000"
 
)
```







Column { data-width=425 }
-----------------------------------------------------------------------

### **Daily confirmed cases in Kenya ** 


```{r}
## day of first case confirmed

day=(covid_set_ke %>% ungroup() %>% filter(daily_conf>0) %>% slice(1) %>% select(date))
## a geom_smooth should also work in this case
daily_conf=covid_set_ke %>% filter(date>=day$date) %>% mutate(daily_conf_smooth=(daily_conf+
              lag(daily_conf,n=1)+
              lag(daily_conf,n=2)+
              lag(daily_conf,n=3)+
              lag(daily_conf,n=4))/5)
daily_conf_p=daily_conf %>% 
  plot_ly(
    x=~date,
    y=~daily_conf,
    type='scatter',
    mode='lines',
    line=list(color="purple",width=0.6),
    name = "Confirmed"
  ) %>% 
  add_trace(
    y=~daily_conf_smooth,
    type="scatter",
    mode="lines",
    line=list(color="blue",width=3),
    name="trendline"
  ) %>% 

  layout(
    title="",
    yaxis=list(
    title="Daily confirmed cases",
    zeroline=F),
    xaxis=list(
      title = "Using 5 days trailing moving average to calculate the trend line",
      zeroline=F
    ),
    legend=list(x=0.1,y=0.9)
    
  )
daily_conf_p
```




### 
```{r}

DT::datatable(covid_set_ke %>% ungroup %>%  select(-c(Country.Region,Week,cum_conf,cum_deaths,cum_recov,cum_active,daily_active)) %>% rename(confirmed=daily_conf,succumbed=daily_deaths,recovered=daily_recov) %>% arrange(desc(date)) %>% slice(1:5),option=list(bpaginate=FALSE))

```






Column { data-width=425 }
-----------------------------------------------------------------------------

### **Daily recovered cases in Kenya **


```{r}
## day of first case confirmed

day=(covid_set_ke %>% ungroup() %>% filter(daily_conf>0) %>% slice(1) %>% select(date))
## a geom_smooth should also work in this case
daily_recov=covid_set_ke %>% filter(date>=day$date) %>% mutate(daily_recov_smooth=(daily_recov+
              lag(daily_recov,n=1)+
              lag(daily_recov,n=2)+
              lag(daily_recov,n=3)+
              lag(daily_recov,n=4))/5)
daily_recov_p=daily_recov %>% 
  plot_ly(
    x=~date,
    y=~daily_recov,
    type='scatter',
    mode='lines',
    line=list(color="blue",alpha=0.1,width=0.6),
    name = "Recovered"
  ) %>% 
  add_trace(
    y=~daily_recov_smooth,
    type="scatter",
    mode="lines",
    line=list(color="green",width=3),
    name="trendline"
  ) %>% 

  layout(
    title="",
    yaxis=list(
    title="Daily Recovered cases",
    zeroline=F),
    xaxis=list(
      title = "Using 5 days trailing moving average to calculate the trend line",
      zeroline=F
    ),
    legend=list(x=0.1,y=0.9)
    
  )
daily_recov_p
```




### **Daily succumbed cases in Kenya**


```{r}
## day of first case confirmed

day=(covid_set_ke %>% ungroup() %>% filter(daily_conf>0) %>% slice(1) %>% select(date))
## a geom_smooth should also work in this case
daily_succumbed=covid_set_ke %>% filter(date>=day$date) %>% mutate(daily_succ_smooth=(daily_deaths+
              lag(daily_deaths,n=1)+
              lag(daily_deaths,n=2)+
              lag(daily_deaths,n=3)+
              lag(daily_deaths,n=4))/5)
daily_succumbed_p=daily_succumbed %>% 
  plot_ly(
  x=~date,
  y=~daily_deaths,
  type="scatter",
  mode="lines",
  line=list(color="blue",width=0.6),
  name="Succumbed") %>% 
  add_trace(
    y=~daily_succ_smooth,
    type="scatter",
    mode="lines",
    line=list(color="red",width=3),
    name="trendline"
  ) %>% 
  layout(
    yaxis=list(
      title="Daily succumbed cases",
      zeroline=F
    ),
    legend=list(x=0.1,y=0.9),
    xaxis=list(
      title = "Using 5 days trailing moving average to calculate the trend line",
      zeroline=F
    )
  )
daily_succumbed_p
```


Combined the above plots using ggplot2 instead of plotly

```{r}
theme_set(theme_light())
colors=c("confirmed"="blue","succumbed"="red","recovered"="green")

## day of first case confirmed

day=(covid_set_ke %>% ungroup() %>% filter(daily_conf>0) %>% slice(1) %>% select(date))



dail=covid_set_ke %>% filter(date>=day$date) %>% 
  ggplot(mapping=aes(x=date))+
  
  geom_line(aes(y=daily_conf,color="confirmed"),lwd=0.7,alpha=0.5)+
  
  geom_smooth(aes(y=daily_conf),lwd=0.3,linetype="dashed",color="black",se=FALSE,alpha=0.5)+
  
  geom_point(aes(x=last(covid_set_ke$date),y=last(covid_set_ke$daily_conf),color="confirmed"),alpha=0.01,size=2.2)+
  
  geom_line(mapping=aes(y=daily_deaths,color="succumbed"),lwd=0.5,alpha=0.8)+
  
  geom_point(aes(x=last(covid_set_ke$date),y=last(covid_set_ke$daily_deaths),color="succumbed"),alpha=0.01,size=2.2)+
  
  geom_line(mapping = aes(y=daily_recov,color="recovered"),lwd=0.8,alpha=0.5)+
  
  geom_point(aes(x=last(covid_set_ke$date),y=last(covid_set_ke$daily_recov),color="recovered"),alpha=0.01,size=2.2)+
  
  
  
 # geom_line(aes(y=Confirmed),lwd=0.4,se=FALSE,alpha=0.5,linetype="dashed",color="blue")+
  
  #geom_point(aes(x=last(sub_set_mod$Date),y=last(sub_set_mod$Confirmed)),color="blue",alpha=0.01,size=10)+
  
  
  
  
  #geom_text(aes(x=last(sub_set_mod$Date),y=last(sub_set_mod$Confirmed),
              #label=paste("Total Cases:",last(sub_set_mod$Confirmed)),
             #alpha=1),nudge_x=-3.0,size=4,colour="black")+
  
#geom_text(aes(x=last(sub_set_mod$Date),y=last(sub_set_mod$Conf_dail),
              #label=last(sub_set_mod$Conf_dail),
             #alpha=1),size=4,colour="black"#nudge_x=-3.5,nudge_y=2,)
#)+
  
  
  labs(ggtitle(""),
       x="Day",
       y="Number of reported cases per day",
       color="Legend")+
  scale_color_manual(values = colors)
          
   #geom_text(
   # label=last(sub_set_mod$Confirmed), 
    #x=last(sub_set_mod$Date),
    #y=last(sub_set_mod$Confirmed),
    #label.padding = unit(0.55, "lines"), # Rectangle size around label
    #label.size = 0.35,
    #color = "black",
    #fill="#69b3a2"
  #)
  
  #geom_text(aes(label=Confirmed), colour="gray20", alpha=1)
  
  
  #text(x=last(sub_set_mod$Date),y=last(sub_set_mod$Confirmed))
#ggplotly(dail)
```










County stats
=====================================================================






```{r}
### **Number of cases per county in Kenya**
# reading in the Kenyan shapefiles
# If reading a shapefile, the data source name (dsn= argument) is the folder (directory) where the shapefile is, and the layer is the name of the shapefile (without the .shp extension)


kenya_shp=readOGR(dsn="C:/Users/ADMIN/Desktop/Intoduction to Python for data science/R for data science/aRduino/Covid datasets/kenyan-counties",layer="County",verbose = F)


# converting the shp file to a df
kenya_df=fortify(kenya_shp) # use broom::tidy(kenya_m) since fortify may be deprecated in the future
kenya_df$id=as.integer(kenya_df$id)


kenya_ctys=read_excel("C:/Users/ADMIN/Desktop/Intoduction to Python for data science/R for data science/aRduino/counties.xlsx")


kenya_ctys %<>% select(-1)



cov_ctys=read.csv("C:/Users/ADMIN/Desktop/Intoduction to Python for data science/R for data science/cov_cty.csv") %>% select(-1)


# for displaying newly confirmed cases and distinguishing them from the previous day's cases

cov_ctys_disp = cov_ctys %>% group_by(county) %>%  select(c(1,ncol(cov_ctys)-1,ncol(cov_ctys))) %>% rename(cases1=2,cases2=3) %>% transmute(cases_today=cases2-cases1) %>% filter(cases_today>0)


kenya_ctys=left_join(kenya_ctys,cov_ctys,by="county")  %>% gather(key="date",value = "cases",-c(1,2,3)) %>%  mutate(date=substr(date,2,11)) %>% mutate(date=ymd(date))

kenya_ctys_filt=kenya_ctys %>% filter(date==max(date))

comp_df= left_join(kenya_df,kenya_ctys_filt,by="id")

comp_df %<>% select(-c(order,hole,piece,id,group)) %>% 
  group_by(county) %>% arrange(desc(cases)) %>% 
  ungroup()


# Rorder data + Add a new column with tooltip text
# with the data I had, I computed the mean coordinates per county to act as my centroid point

kenya_mean= comp_df %>% group_by(county,county_code,cases,date) %>% summarise(long=mean(long),lat=mean(lat)) %>% 
  mutate(tooltip=paste(
    "County: ", county, "\n",
  "Cases: ", cases, sep = ""
  )) %>% 
  filter(cases>0) %>% 
  arrange(desc(cases))


# creating a table that shows the cases confirmed on that day and the cumulative cases

cov_ctys_disp %<>% full_join(kenya_mean,by="county") %>% select(-c(county_code,long,lat,tooltip)) %>% replace_na(list(cases_today=0)) %>% rename(total_cases=cases) %>%  select(c("county","date","cases_today","total_cases"))

```



Column 
-----------------------------------------------------------------------------


### **Number of confirmed cases per county**





```{r,fig.width=14, fig.height=8}
### 
# ### 
# { data-width=400 }
# Now we want to add another information. The number of cases per county will be mapped to the colour and the size of the bubbles. Note that the order of county matters! It is advised to show the most important information on top (center). This can been done sorting your dataset before making the plot.

kenya_map=ggplot() +
  geom_polygon(data=comp_df,aes(x=long,y=lat,group=county_code,fill=county_code),show.legend = FALSE)+
  geom_point(data=kenya_mean,aes(x=long,y=lat,color=cases,size=cases,text=tooltip),alpha=0.9)+
  scale_size_continuous()+
  scale_color_viridis()+
  coord_equal()+
  theme_void()+
  #ylim(-3,2)+
  #xlim(20,40)
  ggtitle(" ")+
  theme(legend.position = "right",plot.title = element_text(hjust = 0.5))+
  ## to change the color of the fill aesthetic
  scale_fill_gradient(low = "#B0BEC5",high = "#B0BEC5",space="Lab")+
  
  labs(
    x="longitude",
    y="latitude"
  )
ggplotly(kenya_map,tooltip = "tooltip")
```



###


```{r}
formattable(cov_ctys_disp,list(date=color_tile("white","#E0E0E0"),cases_today=color_tile("white","#B3E5FC"),area(col=("total_cases"))~ normalize_bar("#FFCDD2",0.2)))
```











Global stats
=======================================================================


Row {data-width=400}
-----------------------------------------------------------------------


```{r}
##------------------------world statistics-----------------------

## let's now do some world stats, such as the total count of the incidents per day and the total cumulative incidents

## let's start with the incidents per day across the world. That should be easy right, we group all the incidents and perform a sum based on each individual day and incident

covid_set_global=covid_set %>% group_by(date) %>% summarise(global_confirmed=sum(daily_conf),global_succumbed=sum(daily_deaths),global_recovered=sum(daily_recov))

## little sanity check to confirm our statistics

#succumbed=covid_set %>% filter(date=="2020-01-22") %>% select(daily_deaths) %>% filter(daily_deaths>=1)

## confirmed=covid_set %>% filter(date=="2020-01-22") %>% select(daily_conf) %>% filter(daily_conf>=1)

```







### Day

```{r}
day=last(covid_set$date)

valueBox(
  day,
   icon="far fa-calendar-plus",
  color="#5C6BC0"
)
```

### Global confirmed cases

```{r}
confirmed=sum(covid_set_global$global_confirmed)
valueBox(
  format(confirmed,big.mark = ","),
   icon = "fas fa-user-md",
  color="#7B1FA2"
 
)
```


### Recovered cases

```{r}
recovered=sum(covid_set_global$global_recovered)
recov_rate=round(100*recovered/sum(covid_set_global$global_confirmed),2)
valueBox(
  cat(format(recovered,big.mark = ","),"(",recov_rate,"%",")",sep=""),
  icon="fas fa-heartbeat",
  color="#00C853"
 
)
```



### Succumbed cases

```{r}
succumbed=sum(covid_set_global$global_succumbed)
death_rate=round(100*succumbed/sum(covid_set_global$global_confirmed),2)
valueBox(
  cat(format(succumbed,big.mark = ","),"(",death_rate,"%",")",sep=""),
  color="#D50000"
 
)
```






Row
-----------------------------------------------------------------------

### **Daily cumulative cases globally: ** 


```{r}
## a function that defines the variable in our dataset that the animation will increment by
accumulate_by <- function(dat, var) {
  var <- lazyeval::f_eval(var, dat)
  lvls <- plotly:::getLevels(var)
  dats <- lapply(seq_along(lvls), function(x) {
    cbind(dat[var %in% lvls[seq(1, x)], ], frame = lvls[[x]])
  })
  dplyr::bind_rows(dats)
} 


#?POSIXct had to convert the date to milliseconds to comply with plotly's animation requirements for dates
covid_set_global=covid_set_global %>% mutate(Count=1:length(covid_set_ke$cum_conf),date_s=as.numeric(as.POSIXct(date,format="%Y/%m/%d"))*1000,cum_conf=cumsum(global_confirmed),cum_succumbed=cumsum(global_succumbed),cum_recov=cumsum(global_recovered))

## our animation will increase by Count, which is basically a day count from date 2020-01-22

fig_glob <- covid_set_global %>% accumulate_by(~`Count`)


fig_glob <- fig_glob %>%
  plot_ly(
    x = ~date_s, 
    y = ~cum_conf,
    #split = ~city,
    frame = ~frame, 
    type = 'scatter',
    mode = 'lines+markers', 
  line = list(color="blue"),
   marker=list(color="blue"),
    #linetype = "dashed"
   name='confirmed'
  )
#fig_glob=fig_glob %>% add_markers(x~last(df2$Date3),
                     # y~last(df2$Conf_dail),
                      #type='scatter',
                     # mode="markers",
                     # marker=list(simplify=F))



fig_glob=fig_glob %>% add_trace(
                      y=~cum_succumbed,
                      mode="lines+markers",
                      frame = ~frame,
                      line = list(color="red"),
                      marker=list(color="red"),
                      name="succumbed") 

fig_glob=fig_glob %>% add_trace(
                      y=~cum_recov,
                      mode="lines+markers",
                      frame~frame,
                      line = list(color="green"),
                     marker=list(color="green"),
                      linetype="dashed",
                      name="recovered")%>% layout(
  
  title="",
  xaxis = list(
    type="date",
    title = "Date",
    zeroline = F
  ),
  yaxis = list(
    title = "Cumulative number of cases",
    zeroline = F
  ),
  hovermode="compared"
)%>% animation_opts( 
  frame = 150, 
  transition =2, 
  redraw = TRUE)

fig_glob
```


Africa stats
=======================================================================


Row {data-width=400}
-----------------------------------------------------------------------

```{r}
##----------------------stats in Africa-------------------------

## Our data is not grouped per continentwise, so how do we know which continent our country belongs to? Well, dont fret: package countrycode will do just that.
#?countrycode()
covid_set_africa=covid_set %>% mutate(continent=countrycode(
 sourcevar = Country.Region,
 origin="country.name",
 destination = "continent"
)) %>% group_by(Country.Region) %>% filter(continent=="Africa")

covid_set_africa_summary=covid_set_africa %>% group_by(date) %>% summarise(africa_confirmed=sum(daily_conf),africa_succumbed=sum(daily_deaths),africa_recovered=sum(daily_recov))

ordered_afrcovid_set=covid_set_africa %>%
group_by(Country.Region) %>% 
summarise(total_confirmed=sum(daily_conf),total_succumbed=sum(daily_deaths),total_recovered=sum(daily_recov)) %>% 
  arrange(desc(total_confirmed))


```

### Day

```{r}
day=last(covid_set$date)

valueBox(
  day,
   icon="far fa-calendar-plus",
  color="#5C6BC0"
  
  
)
```

### Confirmed cases in Africa

```{r}
confirmed=sum(covid_set_africa_summary$africa_confirmed)
valueBox(
  format(confirmed,big.mark = ","),
   icon = "fas fa-user-md",
   color="#7B1FA2"
  
 
)
```


### Recovered cases

```{r}
recovered=sum(covid_set_africa_summary$africa_recovered)


recov_rate=round(100*(recovered)/sum(covid_set_africa_summary$africa_confirmed),2)

valueBox(
  cat(format(recovered,big.mark = ","),"(",recov_rate,"%",")",sep=""),
  icon="fas fa-heartbeat",
  color="#00C853"
)
```





### Succumbed cases

```{r}
succumbed=sum(covid_set_africa_summary$africa_succumbed)

death_rate=round(100*(succumbed)/sum(covid_set_africa_summary$africa_confirmed),2)

valueBox(
  cat(format(succumbed,big.mark = ","),"(",death_rate,"%",")",sep=""),
  color="#D50000"
)
```








Column
-------------------------------------
   
### **Daily cumulative cases in Africa: **

```{r,fig.width=5, fig.height=7}
## a function that defines the variable in our dataset that the animation will increment by
# accumulate_by <- function(dat, var) {
#   var <- lazyeval::f_eval(var, dat)
#   lvls <- plotly:::getLevels(var)
#   dats <- lapply(seq_along(lvls), function(x) {
#     cbind(dat[var %in% lvls[seq(1, x)], ], frame = lvls[[x]])
#   })
#   dplyr::bind_rows(dats)
# } 


#?POSIXct had to convert the date to milliseconds to comply with plotly's animation requirements for dates
covid_set_africa_summary=covid_set_africa_summary %>% mutate(Count=1:length(covid_set_ke$cum_conf),date_s=as.numeric(as.POSIXct(date,format="%Y/%m/%d"))*1000,cum_conf=cumsum(africa_confirmed),cum_succumbed=cumsum(africa_succumbed),cum_recov=cumsum(africa_recovered))



#fig_africa <- covid_set_africa_summary %>% accumulate_by(~`Count`)


fig_africa <- covid_set_africa_summary %>%
  plot_ly(
    x = ~date_s, 
    y = ~cum_conf,
    #split = ~city,
    #frame = ~frame, 
    type = 'scatter',
    mode = 'lines+markers', 
    colors="set3",
   # color= "orange",
    line = list(color="blue"),
   marker=list(color="blue"),
    #linetype = "dashed"
   name='confirmed'
  )
#fig_glob=fig_glob %>% add_markers(x~last(df2$Date3),
                     # y~last(df2$Conf_dail),
                      #type='scatter',
                     # mode="markers",
                     # marker=list(simplify=F))



fig_africa=fig_africa %>% add_trace(
                      y=~cum_succumbed,
                      mode="lines+markers",
                      #frame = ~frame,
                      line=list(color="red"),
                      marker=list(color="red"),
                      name="succumbed")

fig_africa=fig_africa %>% add_trace(
                      y=~cum_recov,
                      mode="lines+markers",
                      #frame~frame,
                      line=list(color="green"),
                      marker=list(color="green"),
                      linetype="dashed",
                      name="recovered") %>% #add_annotations(x =                            last(covid_set_africa_summary$date),                                 y=sum(covid_set_africa_summary$africa_confirmed)-last(covid_set_africa_summary$africa_confirmed),
                 # text = last(covid_set_africa_summary$africa_confirmed),
                  #xref = "x",
                  #yref = "y",
                  #showarrow = TRUE,
                  #arrowhead = 4,
                  #arrowsize = .5,
                  #ax = 0,
                  #ay = 0) %>% 
  layout(
  
  title="",
  xaxis = list(
    type="date",
    title = "Date",
    zeroline = F
  ),
  yaxis = list(
    title = "Cumulative number of cases",
    zeroline = F
  ),
  legend=list(x=0.1,y=0.9),
  hovermode="compared")
# )#%>% animation_opts( 
#   frame = 150, 
#   transition =0, 
#   redraw = TRUE) %>% animation_slider(hide=FALSE)

fig_africa
```   

 
###
    

```{r}
## this is quite a long piping operation but very easy in itself, just execute each part independently to ease understanding

afrcov_summary=covid_set_africa %>% ungroup() %>% group_by(Country.Region) %>% filter(date==last(date)) %>% select(-c(daily_active,Week,continent,cum_active,date)) %>% arrange(desc(cum_conf))%>% rename(daily_confirmed=daily_conf,daily_succumbed=daily_deaths,daily_recovered=daily_recov,total_confirmed=cum_conf,total_succumbed=cum_deaths,total_recovered=cum_recov) %>% select(c("Country.Region","total_confirmed","daily_confirmed","total_recovered","daily_recovered","total_succumbed","daily_succumbed"))

## a better approach to arranging columns is by using dplyr::relocate rather than: select(c("Country.Region","total_confirmed","daily_confirmed","total_recovered","daily_recovered","total_succumbed","daily_succumbed")) 

DT::datatable(afrcov_summary,option=list(bpaginate=FALSE))

```




Protective measures
=======================================================================

Row {data-width=400}
-----------------------------------------------------------------------


### How to protect yourself and others

```{r}
#day=last(covid_set$date)

valueBox(
  paste("Here are some guidelines from the WHO")
  
)
```

Column
-----------------------------------------------------------------------
### **Wash your hands frequently**
Regularly and thoroughly clean your hands with an alcohol-based hand rub or wash them with soap and water.

**Why?** Washing your hands with soap and water or using alcohol-based hand rub kills viruses that may be on your hands.




### **Maintain social distancing**
Maintain at least 1 metre (3 feet) distance between yourself and anyone who is coughing or sneezing.

**Why?** When someone coughs or sneezes they spray small liquid droplets from their nose or mouth which may contain virus. If you are too close, you can breathe in the droplets, including the COVID-19 virus if the person coughing has the disease.


Column
-----------------------------------------------------------------------

### **If you have fever, cough and difficulty breathing, seek medical care early**
Stay home if you feel unwell. If you have a fever, cough and difficulty breathing, seek medical attention and call in advance. Follow the directions of your local health authority.

**Why?** National and local authorities will have the most up to date information on the situation in your area. Calling in advance will allow your health care provider to quickly direct you to the right health facility. This will also protect you and help prevent spread of viruses and other infections.


### **Stay informed and follow advice given by your healthcare provider,your national and local public health authority**

Kindly visit: ***https://www.who.int/emergencies/diseases/novel-coronavirus-2019***
for more information about the Coronavirus disease (COVID-19) Pandemic.

About
=======================================================================

**The COVID-19 in Kenya Dashboard**

Last updated: `r format(max(coronavirus::coronavirus$date), '%d %B')`

This Coronavirus dashboard provides an overview of the 2019 Novel Coronavirus COVID-19 (2019-nCoV) epidemic. This dashboard mainly focuses on Kenya but also gives some brief statistics about the prevalence of the pandemic in the world and Africa. This dashboard is built with R using the R Makrdown framework and was adapted from this [dashboard](https://ramikrispin.github.io/coronavirus_dashboard/){target="_blank"} by Rami Krispin.



**Data**

The input data for this dashboard is the [coronavirus](https://github.com/RamiKrispin/coronavirus) R package (dev version) by Rami Krispin and the [Ministry of Health, Kenya](http://www.health.go.ke/).The data and dashboard is refreshed on a daily bases. The raw data pulled from the Johns Hopkins University Center for Systems Science and Engineering (JHU CCSE) Coronavirus [repository](https://github.com/RamiKrispin/coronavirus-csv)




**Contribution** 

The shape files for the **county stats** tab were contributed by [Maggie Wanjiru](https://twitter.com/magwanjiru). Thank you!